//
//  DataService.m
//  Menu
//
//  Created by trimad on 11-11-16.
//  Copyright 2011 Trimad Group. All rights reserved.
//

#import "DataService.h"

#import "JSON.h"

#import "Category.h"
#import "Item.h"
#import "Util.h"

@interface DataService(Private)

- (NSString *)getCategoryItemsJsonDataFromService;
- (NSArray *)getCategoryItemsFromService;
- (UIImage *)getImageDataFromServiceByName:(NSString *)imageName;

@end

@implementation DataService

@synthesize topCategories;

static DataService *_instance = nil;

+ (DataService *)instance {
	if (!_instance) {
		@synchronized(self) {
			if (!_instance) {
				_instance = [[DataService alloc] init];
			}
		}
	}
	return _instance;
}

- (id)init {
	self = [super init];
	topCategories = [self getCategoryItemsFromService];
	return self;
}

- (NSString *)getCategoryItemsJsonDataFromService {
	//NSLog(@"getCategoryItemsJsonDataFromService");
	NSString* tempStr = [Util readFileForString:@"localCategoryItemsJsonData"];
	if (tempStr != nil) {
		return tempStr;
	} else {
		return [Util readResourceForString:@"getInitCategoryItems.jsp"];
//		NSString* urlString = [[Util getServiceHost] stringByAppendingString:[[NSString alloc] initWithString:@"getInitCategoryItems.jsp"]];
//		tempStr = [Util getResponseByURL:urlString];
//		[Util writeFileForString:tempStr :@"localCategoryItemsJsonData"];
//		return tempStr;
	}
}

- (NSArray *)getCategoryItemsFromService {
	//NSLog(@"getCategoryItemsFromService");
	NSMutableArray *categoryItems = [[NSMutableArray alloc] init];
	NSString* tempStr = [self getCategoryItemsJsonDataFromService];
	NSArray *formatString = (NSArray *)[tempStr JSONValue];
	for (id category in formatString) {
		NSDictionary *tempDic1 = (NSDictionary *)category;
		Category *tempCategory = [[Category alloc] init];
		tempCategory.uid = [[tempDic1 objectForKey:@"uid"] intValue];
		tempCategory.code = [tempDic1 objectForKey:@"code"];
		tempCategory.name = [tempDic1 objectForKey:@"name"];
		NSMutableArray *categoryItemArray = [[NSMutableArray alloc] init];
		
		NSArray *subCategoryArray = [tempDic1 objectForKey:@"subcategories"];
		for (id subCategory in subCategoryArray) {
			NSDictionary *tempDic2 = (NSDictionary *)subCategory;
			Category *tempSubCategory = [[Category alloc] init];
			tempSubCategory.uid = [[tempDic2 objectForKey:@"uid"] intValue];
			tempSubCategory.code = [tempDic2 objectForKey:@"code"];
			tempSubCategory.name = [tempDic2 objectForKey:@"name"];
			tempSubCategory.parentCategory = tempCategory;
			NSMutableArray *subCategoryItemArray = [[NSMutableArray alloc] init];
			
			NSArray *itemArray = [tempDic2 objectForKey:@"items"];
			for (id item in itemArray) {
				NSDictionary *tempDic3 = (NSDictionary *)item;
				Item *tempItem = [[Item alloc] init];
				tempItem.uid = [[tempDic3 objectForKey:@"uid"] intValue];
				tempItem.code = [tempDic3 objectForKey:@"code"];
				tempItem.name = [tempDic3 objectForKey:@"name"];
				tempItem.largeImageName = [tempDic3 objectForKey:@"largeImage"];
				[self getImageDataFromServiceByName:tempItem.largeImageName];
				tempItem.midImageName = [tempDic3 objectForKey:@"middleImage"];
				[self getImageDataFromServiceByName:tempItem.midImageName];
				tempItem.smallImageName = [tempDic3 objectForKey:@"smallImage"];
				[self getImageDataFromServiceByName:tempItem.smallImageName];
				tempItem.price = [[tempDic3 objectForKey:@"price"] intValue];
				tempItem.unit = [tempDic3 objectForKey:@"unit"];
				tempItem.isRecommended = [[tempDic3 objectForKey:@"recommended"] boolValue];
				tempItem.cookMethods = (NSArray*)[tempDic3 objectForKey:@"cookMethods"];
				[subCategoryItemArray addObject:tempItem];
			}
			tempSubCategory.items = subCategoryItemArray;
			[categoryItemArray addObject:tempSubCategory];
		}
		tempCategory.subCategories = categoryItemArray;
		[categoryItems addObject:tempCategory];
	}
	//NSLog(@"categoryItems: %@",categoryItems);
	return categoryItems;
}

- (UIImage *)getImageDataFromServiceByName:(NSString *)imageName {
	//NSLog(@"getImageDataFromServiceByName");
	UIImage *tempImage = [Util readFileForImage:imageName];
	if (tempImage != nil) {
		return tempImage;
	} else {
		return [Util readResourceForImage:imageName];
//		NSString *relativeString = @"catering/images/";
//		//NSString *relativeString = @"";
//		relativeString = [relativeString stringByAppendingString:imageName];
//		
//		NSString* urlString = [[Util getServiceHost] stringByAppendingString:[[NSString alloc] initWithString:relativeString]];
//		tempImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
//		NSLog(@"url %@ %@",urlString,tempImage);
//		[Util writeFileForImage:tempImage:imageName];
//		return tempImage;
	}
}

- (void)dealloc {
	[topCategories release];
	[super dealloc];
}

@end
